// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import VueRouter from 'vue-router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import '@/sass/element-variables.scss'

import StoreConfig from './store/store'
import Util from './libs/util'
import RouterConfig from './router/config'
import Axios from './libs/axios'
import App from './App'
// import Customfilter from '@/libs/customfilter'

Vue.use(Vuex)
Vue.use(VueRouter)
Vue.use(ElementUI)

// 路由配置
const router = new VueRouter({
  // mode: 'history',
  // history: true,
  // saveScrollPosition: true,
  // transitionOnLoad: true,
  routes: RouterConfig
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
  if (to.meta.auth) {
    if (Util.getlocalStorage('user')) {
      if (to.name == 'Login' || to.name == 'Register') {
        next({
          path: '/'
        })
      } else {
        next()
      }
    } else {
      next({
        name: 'Login'
      })
    }
  } else {
    if (Util.getlocalStorage('user')) {
      if (to.name == 'Login' || to.name == 'Register') {
        next({
          path: '/'
        })
      } else {
        next()
      }
    } else {
      next()
    }
  }
})

router.afterEach((to, from) => {
  // 设置当前页面标题
  if (to.meta && to.meta.title) {
    Util.title(to.meta.title)
  }
})

const store = new Vuex.Store(StoreConfig)

Vue.config.productionTip = false

const VM = new Vue({
  el: '#app',
  router,
  store: store,
  components: {
    App
  },
  template: '<App/>'
})

// 注入对象
Axios.init(router, store, VM)

export default VM
